package com.lframework.xingyun.basedata.mappers;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.lframework.starter.web.core.mapper.BaseMapper;
import com.lframework.xingyun.basedata.entity.MemberLevel;
import com.lframework.xingyun.basedata.vo.member.QueryMemberLevelSelectorVo;
import com.lframework.xingyun.basedata.vo.member.QueryMemberLevelVo;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 会员等级配置 Mapper 接口
 * </p>
 *
 * @author mos
 */
public interface MemberLevelMapper extends BaseMapper<MemberLevel> {

    /**
     * 查询列表
     * @param vo
     * @return
     */
    default List<MemberLevel> queryList(QueryMemberLevelVo vo) {
        return defaultJoinWrapper()
                .eq(Objects.nonNull(vo.getLevel()), MemberLevel::getLevel, vo.getLevel())
                .like(StrUtil.isNotBlank(vo.getLevelName()), MemberLevel::getLevelName, vo.getLevelName())
                .eq(Objects.nonNull(vo.getUpgradeType()), MemberLevel::getUpgradeType, vo.getUpgradeType())
                .eq(Objects.nonNull(vo.getAvailable()), MemberLevel::getAvailable, vo.getAvailable())
                .orderByAsc(MemberLevel::getLevel)
                .list();
    }

    /**
     * 查询列表
     * @param vo
     * @return
     */
    default IPage<MemberLevel> query(QueryMemberLevelVo vo) {
        return defaultJoinWrapper()
                .eq(Objects.nonNull(vo.getLevel()), MemberLevel::getLevel, vo.getLevel())
                .like(StrUtil.isNotBlank(vo.getLevelName()), MemberLevel::getLevelName, vo.getLevelName())
                .eq(Objects.nonNull(vo.getUpgradeType()), MemberLevel::getUpgradeType, vo.getUpgradeType())
                .eq(Objects.nonNull(vo.getAvailable()), MemberLevel::getAvailable, vo.getAvailable())
                .orderByAsc(MemberLevel::getLevel)
                .page(PageDTO.of(vo.getPageIndex(), vo.getPageSize()));
    }

    /**
     * 选择器查询
     * @param vo
     * @return
     */
    default IPage<MemberLevel> selector(QueryMemberLevelSelectorVo vo) {
        return defaultJoinWrapper()
                .eq(Objects.nonNull(vo.getLevel()), MemberLevel::getLevel, vo.getLevel())
                .like(StrUtil.isNotBlank(vo.getLevelName()), MemberLevel::getLevelName, vo.getLevelName())
                .eq(Objects.nonNull(vo.getUpgradeType()), MemberLevel::getUpgradeType, vo.getUpgradeType())
                .eq(Objects.nonNull(vo.getAvailable()), MemberLevel::getAvailable, vo.getAvailable())
                .page(PageDTO.of(vo.getPageIndex(), vo.getPageSize()));
    }

    /**
     * 查询永久会员等级
     * @return
     */
    default MemberLevel queryForever() {
        return defaultJoinWrapper()
                .eq(MemberLevel::getEffectiveDay, 0)
                .first(MemberLevel.class);
    }
}

